R Markdown Presentation & Plotly Plots Brian Gue Sept 11, 2017
The data is a subset of the ggplot2 sample diamonds dataset.
library(ggplot2)
data(diamonds)
d <- diamonds[sample(nrow(diamonds), 50), ]
summary(d)## carat cut color clarity depth
## Min. :0.2300 Fair : 2 D:6 SI2 :17 Min. :58.20
## 1st Qu.:0.4100 Good : 3 E:7 SI1 :11 1st Qu.:60.75
## Median :0.7150 Very Good:15 F:8 VS2 :10 Median :61.60
## Mean :0.8482 Premium :12 G:7 VS1 : 6 Mean :61.55
## 3rd Qu.:1.0775 Ideal :18 H:7 VVS2 : 4 3rd Qu.:62.70
## Max. :2.4200 I:9 I1 : 2 Max. :64.50
## J:6 (Other): 0
## table price x y
## Min. :55.00 Min. : 455 Min. :3.900 Min. :3.950
## 1st Qu.:56.00 1st Qu.: 964 1st Qu.:4.763 1st Qu.:4.753
## Median :57.00 Median : 2454 Median :5.775 Median :5.780
## Mean :58.03 Mean : 3455 Mean :5.852 Mean :5.842
## 3rd Qu.:59.00 3rd Qu.: 4489 3rd Qu.:6.617 3rd Qu.:6.577
## Max. :68.00 Max. :16198 Max. :8.850 Max. :8.820
##
## z
## Min. :2.460
## 1st Qu.:2.955
## Median :3.540
## Mean :3.596
## 3rd Qu.:4.050
## Max. :5.170
##
d## # A tibble: 50 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 2.03 Ideal G SI2 61.5 57 12154 8.15 8.07 4.99
## 2 1.02 Premium D SI2 62.8 55 5055 6.46 6.38 4.03
## 3 0.71 Very Good E SI2 62.7 59 2397 5.70 5.75 3.59
## 4 0.41 Ideal J VS1 62.7 57 830 4.75 4.72 2.97
## 5 1.50 Very Good J VS1 61.3 59 8662 7.36 7.41 4.53
## 6 0.41 Premium E VS1 60.7 59 964 4.77 4.79 2.90
## 7 0.41 Very Good D VS2 63.8 56 871 4.69 4.74 3.01
## 8 1.04 Ideal H SI2 60.5 57 3786 6.55 6.51 3.95
## 9 0.81 Premium I SI2 61.5 60 1786 5.94 5.90 3.64
## 10 1.35 Very Good I SI2 58.6 63 5579 7.28 7.19 4.24
## # ... with 40 more rows
Here’s a plot generated by plotly. With a few lines, we get hoverovers for both features of the dataset (prce, and carat).
on the chart, both color and size indicate carat.
library(plotly)##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- plot_ly(
d, x = ~carat, y = ~price,
# Hover text:
text = ~paste("Price: ", price, '$<br>Cut:', cut),
color = ~carat, size = ~carat
)## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
The chart itself repeats one you can find in the plotly documentation, at https://plot.ly/r/line-and-scatter/ .